package org.erepublik.company.service;

import java.util.ArrayList;
import java.util.Date;

import org.erepublik.company.Battle;
import org.erepublik.company.Hospital;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONBattleService extends JSONProductService<Battle> {

    public JSONBattleService() {
        super("activeBattles");
    }

    @Override
    protected Battle[] getEmptyTable() {
        return new Battle[0];
    }

    @Override
    protected Battle[] getProductFromJSONArray(JSONArray jsonArray) throws JSONException {
        return getBattlesFromJSONArray(jsonArray).toArray(new Battle[0]);
    }

    

    
    public static ArrayList<Battle> getBattlesFromJSONArray(JSONArray jsonArray) throws JSONException {
        ArrayList<Battle> data = new ArrayList<Battle>();
        for (int i = 0; i < jsonArray.length(); i++) {
            data.add( Battle.fromJSONObject( (JSONObject)jsonArray.get(i) ) );
        }
        return data;
    }  
    
    
    public static void main(String[] args) {
        JSONHospitalService hospitalService = new JSONHospitalService();
        JSONBattleService battlleService = new JSONBattleService();
        
        for (Battle battle : battlleService.allProducts) {
            System.out.println(battle.getAttacker() + "( "+battle.getAttackersAllies().size()+" )" + " vs "+ battle.getDefender()   +  "( "+battle.getDefendersAllies().size()+" )"  +  " " + new Date(battle.getBattleStartTime()));
            ArrayList<Hospital> hospitals = hospitalService.getHospitalsInRegion(battle.getRegion());
            for (Hospital hospital : hospitals) {
                hospital.setUsageTime( (hospital.getRegion()-1)*1000*60*60*2l + battle.getBattleStartTime() + 1000l*60l*60l*9l );
                System.out.println(new Date(hospital.getUsageTime()) + " " + hospital.getWellness() + " " + hospital.getWellnessPerPlayer());
            }
        
        }
    }
    
}
